package com.aabte.auth.group.infrastructure.messaging.producer;

import com.aabte.auth.group.application.GroupEventPublisher;
import com.aabte.auth.group.domain.event.GroupCreated;
import com.aabte.auth.group.domain.model.Group;
import com.aabte.auth.tracker.model.Status;
import com.aabte.auth.tracker.model.Tracker;
import com.aabte.auth.tracker.repository.TrackerRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Instant;

@Component
public class GroupEventProducerImpl implements GroupEventPublisher {

    @Resource
    private ApplicationContext context;

    @Resource
    private TrackerRepository trackerRepository;

    @Override
    public void groupCreated(Group group) {
        GroupCreated groupCreated = new GroupCreated(group.getGroupId(), group.getGroupName());
        context.publishEvent(groupCreated);

        Tracker tracker = new Tracker(trackerRepository.nextTrackerId(), groupCreated.identity(), Instant.now(),
                null, 0, Status.PROCESSING);
        trackerRepository.store(tracker);
    }

}
